﻿using System;
using System.Web;
using System.Web.Security;
using Core.Library;
using DataObject;
using DataObject.Members;
using DataObject.Photo;
using DataObject.Ref;

namespace Event
{
    public partial class Album2 : System.Web.UI.Page
    {
        #region variables, constants, properties
        protected int _round;
        protected string _email;
        protected int _id;
        private string BUTTON_UPLOAD = @"<a href=""/sua-thong-tin.htm"" class=""form-upload-btn ml300"">Upload bài thi</a>";
        private int topInSlide = 5;
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            _round = Utils.GetIdFromRequestString(Utils.Round, 1);
            _email = Utils.GetIdFromRequestString(Utils.Email, string.Empty);
            _id = Utils.GetIdFromRequestString(Utils.Id, 0);
            if (!IsPostBack)
            {
                #region infor, comment, list photo

                {
                    if (_email != string.Empty)
                    {
                        var dt = MembersCache.Members_GetByIdFrontEnd(_round, _id);
                        this.ctrInfor.Visible = this.ctrInfor.DataBind(dt, _round);
                        this.ctrComment.Visible = this.ctrComment.DataBind(dt);

                        var inSlide = MembersCache.Members_InSlide(topInSlide, _round);
                        if (inSlide != null && inSlide.Rows.Count > 0)
                        {
                            this.ctrMemberInSlide.Visible = this.ctrMemberInSlide.DataBind(inSlide);
                        }

                        if (dt != null && dt.Rows.Count > 0)
                        {
                            int memberRound = ConvertUtility.ConvertTo<int>(dt.Rows[0][Constants.Table_Members.Member_InRound]);

                            //phRound2.Visible = RefCache.Ref_GetCurrentRound() > 1 && memberRound > 1;

                            if (memberRound < _round) Channelvn.Cached.UrlRewrite._301Redirection._404Redirect();

                            int id = ConvertUtility.ConvertTo<int>(dt.Rows[0][Constants.Table_Members.Member_ID]);

                            string _Email = string.Empty;

                            if (HttpContext.Current.Request.IsAuthenticated)
                            {
                                FormsIdentity ident = HttpContext.Current.User.Identity as FormsIdentity;
                                if (ident != null)
                                {
                                    try
                                    {
                                        FormsAuthenticationTicket ticket1 = ident.Ticket;
                                        string[] userData = ticket1.UserData.Split('|');
                                        _Email = userData[1];
                                    }
                                    catch (Exception)
                                    {
                                    }
                                }
                            }
                            else if (HttpContext.Current.Session[Const.AuthenticationSessionName] != null)
                            {
                                string[] userData = HttpContext.Current.Session[Const.AuthenticationSessionName].ToString().Split('|');
                                _Email = userData[1];
                            }

                            if (!string.IsNullOrWhiteSpace(_Email) && Utils.CutEmail(_Email) == _email && RefCache.Ref_GetLockUploadStatus() && memberRound >= RefCache.Ref_GetCurrentRound())
                            {
                                var count = PhotoCache.Photos_CountByMemberID(id, _round);
                                if (count == 0)
                                {
                                    ltrNoPhoto.Text = BUTTON_UPLOAD;
                                    ltrNoPhoto.Visible = true;
                                }
                            }



                            var dtPhoto = PhotoCache.Photos_GetByMemberID(id, _round);
                            ctrListPhoto.Visible = this.ctrListPhoto.DataBind(dtPhoto, dt, _round);


                            #region Quang cao flash
                            {
                                ltrAdvLeft.Text = Utils.BuildFlash("advLeft", "/images/banner-bottom-profile-600x120.swf", string.Empty, 600, 120);
                            }
                            #endregion
                            return;
                        }

                    }
                }
                #endregion
            }
            Channelvn.Cached.UrlRewrite._301Redirection._404Redirect();
        }
    }
}